package com.kuaiyin.controller.jmeter;


import com.kuaiyin.dto.JMeterTestDTO;
import com.kuaiyin.entity.JmeterTest;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.IJmeterTestService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.JMeterTestVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * JMeter测试配置表 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-06-23
 */
@Slf4j
@Tag(name = "压测管理")
@RestController
@RequestMapping("/jmeter")
public class JmeterTestController {

    @Autowired
    private IJmeterTestService jmeterTestService;

    @Operation(summary = "新增压测计划")
    @PostMapping()
    public Result<String> saveJmeterTest(@Valid @RequestBody JMeterTestDTO jmeterTestDTO) {
        jmeterTestService.saveJmeterTest(jmeterTestDTO);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新压测计划")
    @PutMapping("/{id}")
    public Result<String> updateJmeterTest(@PathVariable Long id, @Valid @RequestBody JMeterTestDTO jmeterTestDTO) {
        jmeterTestService.updateJmeterTest(id, jmeterTestDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除压测计划")
    @DeleteMapping("/{id}")
    public Result<String> deleteJmeterTest(@PathVariable Long id) {
        jmeterTestService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "获取压测计划详情")
    @GetMapping("/{id}")
    public Result<JMeterTestVO> getJmeterTest(@PathVariable Long id) {
        JMeterTestVO jmeterTestVO = jmeterTestService.getById(id);
        return Result.success(jmeterTestVO);
    }

    @Operation(summary = "分页查询压测计划列表")
    @GetMapping("/search")
    public Result<PageResult<JMeterTestVO>> getJmeterTest(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String planId) {
        PageResult<JMeterTestVO> JmeterPage = jmeterTestService.getJmeterTest(page, size, wrapper -> {
            if (StringUtils.hasText(name)) {
                wrapper.like(JmeterTest::getName, name);
            }
            if (StringUtils.hasText(planId)) {
                wrapper.eq(JmeterTest::getPlanId, planId);
            }
            wrapper.orderByDesc(JmeterTest::getId);
        });
        return Result.success(JmeterPage);
    }

    @Operation(summary = "压测列表")
    @GetMapping("/all")
    public Result<List<JMeterTestVO>> getJmeterTestList() {
        List<JMeterTestVO> jmeterTestList = jmeterTestService.getJmeterTestList();
        return Result.success(jmeterTestList);
    }

    @Operation(summary = "运行压测计划")
    @PostMapping("/run/{id}")
    public Result<String> runJmeterTest(@PathVariable Long id) throws Exception {
        jmeterTestService.runJmeterTest(id);
        return Result.success("运行成功，可点击查看报告");
    }
    @Operation(summary = "查看压测报告")
    @GetMapping("/report/{id}")
    public Result<Map<String, String>> reportJmeter(@PathVariable Long id) {
        Map<String, String>  report = jmeterTestService.getReport(id);
        return Result.success(report);
    }

    @Operation(summary = "停止计划运行")
    @PostMapping("/stop/{id}")
    public Result<String> stopTestPlan(@PathVariable Long id) {
        jmeterTestService.stopTestPlan(id);
        return Result.success("停止命令已发送");
    }
}
